﻿@using NewLife;
@using System.ComponentModel;
@using System.Reflection;
@using NewLife.Reflection;
@{
	//Layout = NewLife.Cube.Setting.Current.Layout;

	var obj = (Object)Model;
	var list = ViewBag.Fields as IList<DataField>;
	var dic = list
		.GroupBy(e => e.Category + "")
		.ToDictionary(e => e.Key, e => e.ToList());
}
<div class="row">
	@await Html.PartialAsync("_Object_Nav", obj)
</div>
<div class="row">
	@using (Html.BeginForm("Update", null))
	{
		@Html.AntiForgeryToken()
		@Html.ValidationSummary(true)
		foreach (var item in dic)
		{
			<div class="col-12 mb-3">
				<div class="card">
					@if (dic.Count > 1)
					{
						<div class="card-header">
							<h2 class="card-title">@item.Key</h2>
						</div>
					}
					<div class="card-body">
						@foreach (var pi in item.Value)
						{
							var name = pi.Name;
							var val = obj.GetValue(name);
							var dis = pi.DisplayName;
							var des = pi.Description;
							var dataSource = pi.DataSource;

							<div class="row mb-3">
								<label class="form-label col-xs-2 col-md-3 col-form-label text-end" for="@name">@(dis)</label>
								<div class="col-xs-10 col-md-4">
									@if (dataSource != null)
									{
                                        @await Html.PartialAsync("_Form_DropDownList", new ListBoxModel(name, dataSource(item.Value), val))
									}
									else
									{
										@await Html.PartialAsync("_Form_Editor", new ItemModel(name, val, pi.Type))
									}
									@Html.ValidationMessage(name)
								</div>
								<span class="hidden-xs col-md-5">&nbsp; @des</span>
							</div>
						}
					</div>
				</div>
			</div>
		}
		if (this.Has(PermissionFlags.Update))
		{
			<div class="clearfix form-actions col-sm-12 col-md-12">
				<label class="control-label col-xs-4 col-sm-5 col-md-5"></label>
				<button type="submit" class="btn btn-success"><i class="glyphicon glyphicon-save"></i><strong>保存</strong></button>
				<button type="button" class="btn btn-danger" onclick="history.go(-1);"><i class="glyphicon glyphicon-remove"></i><strong>取消</strong></button>
			</div>
		}
	}
</div>